package com.toy.core.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Update;

import com.mongodb.gridfs.GridFSDBFile;
import com.toy.core.cache.FinderPathAutoUpdater;
import com.toy.core.cache.annotation.CacheEvict;
import com.toy.core.cache.annotation.CachePutGet;
import com.toy.core.dao.MongoCrudRepository;
import com.toy.core.dao.support.Page;
import com.toy.core.domain.BaseObject;
import com.toy.core.service.BaseManager;
import com.toy.core.service.support.GridFSDBFileWrapper;

public abstract class BaseManagerImpl<T extends BaseObject> implements BaseManager<T> {
	
	@Autowired
	private FinderPathAutoUpdater finderPathAutoUpdater;
	
	/*---------------------save方法---------------------*/
	@CacheEvict
	public T saveOrUpdate(T entity) {
		T result = getMongoCrudRepository().save(entity);
		finderPathAutoUpdater.executeUpdate(entity.getClass().getName(), entity);
		return result;
	}
	
	/*---------------------更新方法---------------------*/
	@CacheEvict
	public T updateById(Long id, String propertyName,
			Object newValue) {
		T entity = getMongoCrudRepository().update(id, propertyName, newValue);
		finderPathAutoUpdater.executeUpdate(entity.getClass().getName(), entity);
		return entity;
	}
	
	@CacheEvict
	public T findAndModify(long idValue,Update update){
		T entity = getMongoCrudRepository().findAndModify(idValue, update);
		finderPathAutoUpdater.executeUpdate(entity.getClass().getName(), entity);
		return entity;
	}

	/*---------------------删除方法---------------------*/
	@CacheEvict
	public void delete(Long id){
		getMongoCrudRepository().delete(id);
	}
	
	@CacheEvict
	public void delete(T entity){
		getMongoCrudRepository().delete(entity);
	}
	
	/*---------------------get方法---------------------*/
	@CachePutGet 
	public  T getById(Long id) {
		return getMongoCrudRepository().getById(id);
	}
	
	@CachePutGet 
	public T getUniqueByProperty(String propertyName, Object value) {
		return getMongoCrudRepository().getUniqueByProperty(propertyName,value);
	}
	
	/*---------------------find方法---------------------*/
	@CachePutGet
	public List<T> findByProperty(String propertyName,Object value){
		return getMongoCrudRepository().findByProperty(propertyName, value);
	}
	
	/*----------------------翻页方法--------------------*/
	@CachePutGet
	public Page<T> findPaged(int pageNo, int pageSize){
		return getMongoCrudRepository().findPaged(pageNo, pageSize);
	}
	
	@CachePutGet
	public Page<T> findPaged(List<Criteria> criterions, String orderBy,boolean isAsc,int pageNo, int pageSize){
		return getMongoCrudRepository().findPaged(criterions, orderBy, isAsc, pageNo, pageSize);
	}
	
	/*----------------------翻页方法--------------------*/
	@CachePutGet
	public GridFSDBFileWrapper getGridFSDBFileByObjectId(String objectId) throws Exception{
		GridFSDBFile gridFSDBFile = getMongoCrudRepository().getGridFSDBFileByObjectId(objectId);
		return new GridFSDBFileWrapper(gridFSDBFile);
	}
	
	public abstract MongoCrudRepository<T,Long> getMongoCrudRepository();
}
